package gui;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;

import javax.swing.AbstractAction;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import database.DBconn;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.io.InputStream;

public class SearchFriend extends JFrame {

	private JPanel contentPane;
	private JTable table;
	private JButton btnNewButton;
	PreparedStatement stmt = null;	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args, final String id) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					SearchFriend frame = new SearchFriend(id);
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public SearchFriend(final String id) {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 483, 233);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(0, 11, 467, 183);
		contentPane.add(scrollPane);
		
		table = new JTable();
		table.setModel(new DefaultTableModel(
			new Object[][] {
			},
			new String[] {
				"No.", "Name", "Add"
			}
		) {
			boolean[] columnEditables = new boolean[] {
				false, false, true
			};
			public boolean isCellEditable(int row, int column) {
				return columnEditables[column];
			}
		});
		table.getColumnModel().getColumn(0).setResizable(false);
		table.getColumnModel().getColumn(0).setPreferredWidth(30);
		table.getColumnModel().getColumn(1).setResizable(false);
		table.getColumnModel().getColumn(1).setPreferredWidth(288);
		
		AbstractAction add = new AbstractAction()
		{
		    public void actionPerformed(ActionEvent e)
		    {
		    	 JTable table = (JTable)e.getSource();
		         int modelRow = Integer.valueOf( e.getActionCommand() );
		         int request = 1;
		         int accept = 0;
		         String sql="SELECT * FROM userinfo WHERE u_id = "+modelRow+";";		 	   		 	    
		         try {
		        	 DBconn.getConnection();
		        	 
		        	    Statement stmtName = DBconn.con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
			 	        ResultSet rs = stmtName.executeQuery(sql);
			 	        rs.next();
			 	        String name = rs.getString("u_name");	
			 	       
						stmt = DBconn.con.prepareStatement("insert into friendlist(f_request,f_accept,f_verifyrequest,f_verifyaccept)"+"values(?,?,?,?)");					
						stmt.setString(1,id);
						stmt.setLong(2,modelRow);
						stmt.setLong(3,request);
						stmt.setLong(4,accept);
						stmt.executeUpdate(); 
						
						JOptionPane.showMessageDialog(null,"Your request has been sent to :  "+ name);
						
		 	      	}
		 	    catch(Exception e1) {
		 	        JOptionPane.showMessageDialog(null, e1.getMessage());
		 	    }		         
		         
		    }
		};

		ButtonColumn buttonColumn = new ButtonColumn(table, add, 2);
		buttonColumn.setMnemonic(KeyEvent.VK_D);
				
		scrollPane.setViewportView(table);		
		showFriendList();
	}
	
	private void showFriendList(){
		DefaultTableModel model=(DefaultTableModel) table.getModel();
	    String sql="SELECT * FROM userinfo";
	    try {
	    	//Connection con;
	    	DBconn.getConnection();
		    Statement stmt = DBconn.con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
	        ResultSet rs = stmt.executeQuery(sql);
	      	while(rs.next())
	      	{
	        String id = rs.getString("u_id");	 
	        String name = rs.getString("u_name");	
	        model.addRow(new Object []{id, name});  
	      	}
	         
	    }
	    catch(Exception e1) {
	        JOptionPane.showMessageDialog(null, e1.getMessage());
	    }
	}
	
}
